package com.itheima.controller;

import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.pub.MessageConstant;
import com.itheima.service.PermissionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("permission")
public class PermissionController{

    @Reference
    PermissionService permissionService;

    // 分页查询检查项
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            Map page = permissionService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("addPermission")
    public Result addCheckItem(@RequestBody Permission permission){
        try {
            // saveOrUpdate 此方法主键值为null或者为0为插入,否则为更新
            permissionService.saveOrUpdate(permission);
            return new Result(true, MessageConstant.UPDATE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_CHECKITEM_FAIL);
        }
    }

    // 根据id值删除检查项
    @RequestMapping("deletePermission")
    public Result deleteCheckitemById(@RequestParam("id") Integer id){
        try {
            permissionService.removeById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }
}
